'''
Author: your name
Date: 2021-03-23 09:57:28
LastEditTime: 2021-04-07 10:06:29
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/role/views.py
'''
from flask import render_template,request
from app import db
from .model import Role
from . import auth_role
from .service import RoleService
from .rolemenuservice import RoleMenuService
from .rolemenu import RoleMenu
import json


#获取角色信息的分页列表
@auth_role.route('/pages',methods=['GET','POST'])
def pages():
    if request.method == "POST":
        page = request.form.get("page")  #获取页码
    else:
        page = request.args.get("page")
    pages = RoleService.pages(page) #flask_sqlalchemy提供分页方法.paginate

    data = {
        'msg':'success',
        'code':0,
        'data':Role.list_to_dict(pages.items),
        'count':pages.total
    }
    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出

@auth_role.route('/list',methods=['GET','POST'])
def list():
    lists = RoleService.list() #flask_sqlalchemy提供分页方法.paginate
    data = {
        'msg':'success',
        'code':0,
        'data':Role.list_to_dict(lists),
    }
    return json.dumps(data,ensure_ascii=False) 

@auth_role.route('/save',methods = ['GET','POST'])
def save():
    if request.method == 'POST':
        role_pk = request.form.get('role_pk', None)
        role_name = request.form.get('role_name', None)
        role_time = request.form.get('role_time', None)
        role_status = request.form.get('role_status', None)
        role_remark = request.form.get('role_remark', None)

        newobj = Role()
        newobj.role_name = role_name
        newobj.role_time = role_time
        newobj.role_status = role_status
        newobj.role_remark = role_remark
        if role_pk is not None:
            RoleService.update(role_pk,role_status)
        else:
            RoleService.save(newobj) #保存数据
    data = {
        'msg':'保存成功',
        'code':0,
    }
    return json.dumps(data,ensure_ascii=False) 

@auth_role.route('/delete',methods=['GET','POST'])
def delete():
    if request.method == "POST":
        role_pk = request.form.get("role_pk")  #获取页码
    else:
        role_pk = request.args.get("role_pk")
    RoleService.delete(role_pk) #flask_sqlalchemy提供分页方法.paginate
    data = {
        'msg':'删除成功',
        'code':0,
    }
    return json.dumps(data,ensure_ascii=False) 


@auth_role.route('/saveRole',methods=['GET','POST'])
def saveRole():
    if request.method == "POST":
        fk_roles_pk = request.form.get("role_pk")  #获取页码
        datas = request.form.get("datas")  #获取页码
    else:
        fk_roles_pk = request.args.get("role_pk")
        datas = request.args.get("datas")
    RoleMenuService.delete(fk_roles_pk)
    #todo
    obj = json.loads(datas)
    for item in obj:
        rolemenu = RoleMenu()
        rolemenu.fk_roles_pk = fk_roles_pk
        rolemenu.fk_menu_pk = item['menu_pk']
        rolemenu.checked = '1'
        RoleMenuService.save(rolemenu)
    data = {
        'msg':'授权成功',
        'code':0,
    }
    return json.dumps(data,ensure_ascii=False) 
